/**
 * Copyright(C) 2008 Matthias Braunhofer
 * All rights reserved
 *
 * Created on: 28.07.2008
*/

package it.cums.wizards;

import it.cums.controller.ApplicationController;
import it.cums.model.OrderedProduct;
import it.cums.model.Product;
import it.cums.model.Products;
import it.cums.util.Converter;

import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class OrderedProductWizardPage extends WizardPage implements IWizardPage {
	private Combo productCombo;
	private Text priceText;
	private Text quantityText;
	private Text totalPriceText;
	private Text discountText;
	private Text endPriceText;
	private OrderedProduct product;
	private Composite container;
	
	public OrderedProductWizardPage() {
		super("Add Product");
		setTitle("Add a Product to Order");
		setDescription("Enter/Select a product");
	}
	
	public OrderedProductWizardPage(OrderedProduct product) {
		super("Edit Product");
		setTitle("Edit Product");
		setDescription("Edit Product");
		this.product = product;
	}

	@Override
	public void createControl(Composite parent) {
		container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;
		
		Label label1 = new Label(container, SWT.NULL);
		label1.setText("Product name:");
		productCombo = new Combo(container, SWT.NONE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		productCombo.setLayoutData(gd);
		productCombo.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		productCombo.addFocusListener(new FocusListener() {
			@Override
			public void focusGained(FocusEvent e) {
				// TODO Auto-generated method stub	
			}

			@Override
			public void focusLost(FocusEvent e) {
				setPrice();
			}});
		initializeProducts();
		
		Label label3 = new Label(container, SWT.NULL);
		label3.setText("Price (Euro):");
		priceText = new Text(container, SWT.BORDER | SWT.SINGLE);
		priceText.setLayoutData(gd);
		priceText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label2 = new Label(container, SWT.NULL);
		label2.setText("Quantity:");
		quantityText = new Text(container, SWT.BORDER | SWT.SINGLE);
		quantityText.setLayoutData(gd);
		quantityText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label4 = new Label(container, SWT.NULL);
		label4.setText("Total price (Euro):");
		totalPriceText = new Text(container, SWT.BORDER | SWT.SINGLE);
		totalPriceText.setEditable(false);
		totalPriceText.setLayoutData(gd);
		
		Label label5 = new Label(container, SWT.NULL);
		label5.setText("Discount (%):");
		discountText = new Text(container, SWT.BORDER | SWT.SINGLE);
		discountText.setLayoutData(gd);
		discountText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label6 = new Label(container, SWT.NULL);
		label6.setText("End price (Euro):");
		endPriceText = new Text(container, SWT.BORDER | SWT.SINGLE);
		endPriceText.setEditable(false);
		endPriceText.setLayoutData(gd);
			
		setControl(container);
		initialize();
	}
	
	private void initializeProducts() {
		Products products = ApplicationController.getInstance().getProducts();
		for (Product p : products.getProducts()) {
			productCombo.add(p.getName());
		}
	}
	
	private void setPrice() {
		Products products = ApplicationController.getInstance().getProducts();
		for (Product p : products.getProducts()) {
			if (p.getName().equals(getProduct())) {
				setPrice(p.getSalesPrice());
				updatePrices();
			}
		}
	}
	
	private void initialize() {
		if (product != null) {
			setProduct(product.getProduct());
			setPrice(product.getPrice());
			setQuantity(product.getQuantity());
			setTotalPrice(product.getTotalPrice());
			setDiscount(product.getDiscount());
			setEndPrice(product.getEndPrice());
			setPageComplete(true);
		} else {
			setPageComplete(false);
		}
	}
	
	private void dialogChanged() {
		boolean complete = true;
		if (getProduct().length() == 0) {
			setDescription("Enter/Select a product");
			setErrorMessage(null);
			complete = false;
		}
		if (complete && getPrice().length() == 0) {
			complete = false;
			setDescription("Enter a price");
			setErrorMessage(null);
		}
		if (complete && getPrice().length() != 0) {
			try {
				Converter.convertToDouble(getPrice());
				setErrorMessage(null);
				updatePrices();
			} catch (Exception e) {
				complete = false;
				setErrorMessage("Enter a valid price");
			}
		} 
		if (complete && getQuantity().length() == 0) {
			complete = false;
			setDescription("Enter a quantity");
			setErrorMessage(null);
		}
		if (complete && getQuantity().length() != 0) {
			try {
				Converter.convertToInteger(getQuantity());
				setErrorMessage(null);
				updatePrices();
			} catch (Exception e) {
				complete = false;
				setErrorMessage("Enter a valid quantity");
			}
		} 
		if (complete && getDiscount().length() == 0) {
			complete = false;
			setDescription("Enter a discount");
			setErrorMessage(null);
		}
		if (complete && getDiscount().length() != 0) {
			try {
				Converter.convertToInteger(getDiscount());
				setErrorMessage(null);
				updatePrices();
			} catch (Exception e) {
				complete = false;
				setErrorMessage("Enter a valid discount");
			}
		}
		if (complete) {
			setDescription(null);
			setErrorMessage(null);
		}
		setPageComplete(complete);
	}
	
	private void updatePrices() {
		if (Converter.isDouble(getPrice()) && Converter.isInteger(getQuantity())) {
			double price = Converter.convertToDouble(getPrice());
			int quantity = Converter.convertToInteger(getQuantity());
			double totPrice = price * quantity;
			setTotalPrice(totPrice);
			if (Converter.isInteger(getDiscount())) {
				int discount = Integer.parseInt(getDiscount());
				totPrice = totPrice - (totPrice * discount/100);
			}
			setEndPrice(totPrice);
		}
	}
	
	public String getProduct() {
		return productCombo.getText().trim();
	}
	
	private void setProduct(String product) {
		productCombo.setText(product);
	}
	
	public String getPrice() {
		return priceText.getText().trim();
	}
	
	private void setPrice(double price) {
		priceText.setText(String.valueOf(price));
	}
	
	public String getQuantity() {
		return quantityText.getText().trim();
	}
	
	private void setQuantity(int quantity) {
		quantityText.setText(String.valueOf(quantity));
	}
	
	private void setTotalPrice(double price) {
		totalPriceText.setText(String.valueOf(price));
	}
	
	public String getTotalPrice() {
		return totalPriceText.getText().trim();
	}
	
	public String getDiscount() {
		return discountText.getText().trim();
	}
	
	private void setDiscount(int discount) {
		discountText.setText(String.valueOf(discount));
	}
	
	private void setEndPrice(double totalPrice) {
		endPriceText.setText(String.valueOf(totalPrice));
	}
	
	public String getEndPrice() {
		return endPriceText.getText().trim();
	}
	
	@Override
	public Control getControl() {
		return container;
	}
}
